#!/usr/bin/python

"""
    This file is part of benjap.

    Copyright Graham Jones 2010.

    Benjap is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    banjap is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with benjap (COPYING file).  
    If not, see <http://www.gnu.org/licenses/>.

    $Id$
"""
"""
comet application

"""
import os    

# Start pygame
from appBase import appBase
import pygame
from pygame.locals import QUIT, KEYDOWN, \
    K_ESCAPE, MOUSEBUTTONDOWN, MOUSEMOTION, K_q, K_F4
from random import random, randint, choice


# Make overgrown pixel map
pix_list = []
for rgb_tuple in pygame.color.THECOLORS.values():
    if sum(rgb_tuple) > 509:
        pix_surf = pygame.Surface((5, 5))
        pix_surf.fill(rgb_tuple)
        pix_list.append(pix_surf.convert())
del rgb_tuple, pix_surf

class cometApp(appBase):

    def __init__(self,parent):
    
        print "starsApp.__init__()"
        appBase.__init__(self,parent)

        self.Sprites = pygame.sprite.RenderUpdates()
        self.origin=(int(self.screen_size[0]/2),
                     int(self.screen_size[1]/2))

        musicFile = "sounds/swoosh_noise.wav"
        self.swooshNoise = pygame.mixer.Sound(musicFile)

        self.screen.fill((0,0,0))
        pygame.display.flip()

    def setOrigin(self,pos):
        self.origin = pos

    def processEvent(self,event):
        if event.type == MOUSEMOTION:
            self.swooshNoise.play()
            self.setOrigin(event.pos)
            for n in range(0,10):
                view_time = 800.
                dx = 4.0 * random() - 2.0
                dy = 4.0 * random() - 2.0
                image = choice(pix_list)
                particle(self.origin[0],self.origin[1],
                         dx,dy,
                         view_time,
                         image,
                         self.Sprites)

    def processDisplay(self,delay=0):
        pass



class particle(pygame.sprite.Sprite):
    def __init__(self, x, y, dx,dy, lifetime, image,*groups):
        pygame.sprite.Sprite.__init__(self, *groups)
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy
        self.lifetime = lifetime
        self.image = image
        self.rect = self.image.get_rect(center=(int(x), int(y)))
        self.accel = 0.5
        self.total_time = 0



    def update(self, act_delay):
        if self.total_time < self.lifetime:
            self.dx *= self.accel
            self.dy *= self.accel
            self.x = self.x + act_delay * self.dx
            self.y = self.y + act_delay * self.dy
            self.rect = self.image.get_rect(center=(int(self.x), int(self.y)))
            self.total_time += act_delay
        else:
            self.kill()

